package cook.controllers;

import static cook.utils.Converter.UNITS;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

import java.util.List;

import org.apache.solr.client.solrj.SolrServerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.ImmutableList;

import cook.models.Ingredient;
import cook.models.Recipe;
import cook.services.CookService;

@RestController
public class RecommendationController {
    @Autowired
    private CookService service;


    @RequestMapping(value = "/ingredients", method = GET)
    public List<String> ingredients(@RequestParam(required = false, defaultValue = "-1") final int limit,
            @RequestParam(required = false, defaultValue = "0") final int offset) {
        return service.listIngredients(limit, offset);
    }

    // can be tested with : curl -X POST "http://localhost:8080/recommend" --data
    // "[{\"name\":\"ingr_butter\", \"quantity\":\"39\"}]" --header "Content-Type:application/json"
    @RequestMapping(value = "/recommend", method = POST)
    public List<Recipe> recommend(@RequestBody(required = true) final List<Ingredient> ingredients)
            throws SolrServerException {
        return service.recommend(ingredients, 10, 0);
    }

    @RequestMapping(value = "/units", method = { POST, GET })
    public List<String> units() {
        return ImmutableList.copyOf(UNITS);
    }
}